package com.libing.web;

import com.libing.base.BaseController;
import com.libing.entity.CustomerLevel;
import com.libing.entity.User;
import com.libing.service.CustomerService;
import com.libing.service.UserService;
import com.libing.util.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 登录控制器
 * @Auther: LEE
 * @Date: Create in 2018/3/3 9:55
 */
@Controller
@RequestMapping("/")
public class LoginController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Boolean userLogin(String username, String password, Model model) {
        logger.info("用户登录参数---username:" + username + ",password:" + password);
        User user = userService.queryByName(username);

        if (user != null && user.getPassword().equals(MD5Utils.getMD5(password))) {
            login(user);
            return true;
        } else {
            model.addAttribute("message", "用户名或密码错误！");
            return false;
        }
    }

    @RequestMapping(value = "/autoLogin", method = RequestMethod.POST)
    @ResponseBody
    public Boolean autoLogin(Model model) {

        String username = "admin";
        String password = "e10adc3949ba59abbe56e057f20f883e";
        User user = userService.queryByName(username);

        if (user != null && user.getPassword().equals(MD5Utils.getMD5(password))) {
            login(user);
            return true;
        } else {
            model.addAttribute("message", "用户名或密码错误！");
            return false;
        }
    }

    @GetMapping("/logout")
    public String userLogout() {
        logout();
        return "page-signin";
    }

}
